﻿using System;
using System.Text;
using System.Collections.Generic;
using System.Linq;
using Cms.Utilities;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using Cms.Domain;
using Moq;

namespace Cms.Tests.Unit.Domain
{

    [TestClass]
    public class UserTests
    {
        [TestMethod]
        public void Constructor_CreatedProperty_Now()
        {
            Mock<DateTimeProvider> dateTimeProviderMock = new Mock<DateTimeProvider>();
            DateTime now = new DateTime(2012, 9, 17, 17, 22, 0);
            dateTimeProviderMock.Setup(m => m.Now).Returns(now);
            DateTimeProvider.Current = dateTimeProviderMock.Object;
            User user = new User();
            Assert.AreEqual(now, user.Created);
            DateTimeProvider.ResetToDefaultDateTimeProvider();
        }

        [TestMethod]
        public void Constructor_ForceLogoutProperty_False()
        {
            User user = new User();
            Assert.AreEqual(false, user.ForceLogout);
        }

        [TestMethod]
        public void Constructor_MembershipsProperty_CountZero()
        {
            User user = new User();
            Assert.AreEqual(0, user.Memberships.Count);
        }

        [TestMethod]
        public void Fullname_BothPartsExist_CompleteName()
        {
            User user = new User { Firstname = "Fredrik", Lastname = "Holm" };
            Assert.AreEqual("Fredrik Holm", user.Fullname);
        }

        [TestMethod]
        public void Fullname_OnlyFirstname_Firstname()
        {
            User user = new User { Firstname = "Fredrik" };
            Assert.AreEqual("Fredrik", user.Fullname);
        }

        [TestMethod]
        public void Fullname_OnlyLastname_Lastname()
        {
            User user = new User { Lastname = "Holm" };
            Assert.AreEqual("Holm", user.Fullname);
        }

        [TestMethod]
        public void Fullname_NeitherFirstnameNorLastname_Username()
        {
            User user = new User { Username = "fh" };
            Assert.AreEqual("fh", user.Fullname);
        }

        [TestMethod]
        public void PhonebookName_BothPartsExist_CompleteCommaSeparatedNameLastnameFirst()
        {
            User user = new User { Firstname = "Fredrik", Lastname = "Holm" };
            Assert.AreEqual("Holm, Fredrik", user.PhonebookName);
        }

        [TestMethod]
        public void PhonebookName_OnlyFirstname_Firstname()
        {
            User user = new User { Firstname = "Fredrik" };
            Assert.AreEqual("Fredrik", user.PhonebookName);
        }

        [TestMethod]
        public void PhonebookName_OnlyLastname_Lastname()
        {
            User user = new User { Lastname = "Holm" };
            Assert.AreEqual("Holm", user.PhonebookName);
        }

        [TestMethod]
        public void PhonebookName_NeitherFirstnameNorLastname_StringEmpty()
        {
            User user = new User { Username = "fh" };
            Assert.AreEqual("", user.PhonebookName);
        }

        [TestMethod]
        public void GetMembership_IdExists_MembershipReturned()
        {
            User user = new User();
            Membership m1 = new Membership() { ID = 1 };
            user.Memberships.Add(m1);
            Membership m2 = new Membership() { ID = 2 };
            user.Memberships.Add(m2);
            Membership m3 = new Membership() { ID = 3 };
            user.Memberships.Add(m3);
            Assert.AreSame(m2, user.GetMembership(2));
        }

        [TestMethod]
        public void GetMembership_IdDoesNotExist_NullReturned()
        {
            User user = new User();
            Membership m1 = new Membership() { ID = 1 };
            user.Memberships.Add(m1);
            Membership m2 = new Membership() { ID = 2 };
            user.Memberships.Add(m2);
            Assert.IsNull(user.GetMembership(266));
        }
    }
}
